<?php


namespace App\Http\Controllers\Mini;


use App\Http\Controllers\Controller;
use App\Models\Comment;
use App\Models\CommentNotice;
use \Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

class CommentController extends Controller
{
    private $user_id;

    public function __construct()
    {
        $id = auth('user')->id();

        $this->user_id = $id;
    }

    //邻里圈的评论  TODO:后期需要跟用户表关联 使用with预加载即可  已处理
    public function index($id)
    {
        $data = Comment::query()->with('user:id,username,nickname')->select('id', 'user_id', 'circle_id', 'pid', 'content', 'created_at')->where('circle_id', $id)->get();
       // $data = $this->tree($data);
        return $this->success($data, '请求成功');
    }

    //发起评论
    public function store(Request $request)
    {
        $user_id = $this->user_id;
        if (!$user_id) {
            return $this->error('请先登录');
        }

        $data['circle_id'] = $request->post('circle_id');
        $data['pid'] = $request->post('pid', 0);
        $data['content'] = $request->post('content');

        $validator = Validator::make($data, [
            'circle_id' => 'required|integer',
            'content' => 'required'
        ]);

        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }

        DB::beginTransaction();
        try {
            $data['user_id'] = $user_id;

            Comment::query()->create($data);


            if ($data['pid']) {
                //查询被回复id是谁发布的
                $user = $this->selectPid($data['pid']);

                $commentNotice = [
                    'user_id' => $user,
                    'from_user_id' => $user_id,
                    'msg' => '评论了你的说说',
                    'circle_id' => $data['circle_id']
                ];
                CommentNotice::query()->create($commentNotice);
            }
            DB::commit();
        } catch (\Exception $exception) {
            DB::rollBack();
            return $this->error($exception->getMessage());
        }

        return $this->success([], '评论成功');
    }

    //标记为已读
    public function readNotice(Request $request)
    {
        $user_id = $this->user_id;
        if (!$user_id) {
            return $this->error('请先登录');
        }

        $ids = $request->get('ids');
        $ids = explode(',', $ids);

        $res = CommentNotice::query()->whereIn('id', $ids)->update(['read_at' => date('Y-m-d H:i:s')]);

        if ($res) {
            return $this->success([], '标记成功');
        }
        return $this->error();
    }

    //删除评论
    public function delete($id)
    {
        $user_id = $this->user_id;
        if (!$user_id) {
            return $this->error('请先登录');
        }

        Comment::query()->where('id', $id)->delete();

        return $this->success([], '删除成功');
    }


    /**
     * 递归
     * @param $data
     * @param int $pid
     * @return array
     */
    private static function tree($data, $pid = 0)
    {
        $res = array();

        foreach ($data as $key => $value) {
            if ($value['pid'] == $pid) {
                $children = self::tree($data, $value['id']);
                if (!empty($children)) {
                    $value['children'] = $children;
                }
                $res[] = $value;
            }
        }
        return $res;
    }


    //获取用户的通知
    public function notice()
    {
        $user_id = $this->user_id;

        if (!$user_id) {
            return $this->error('请先登录');
        }

        $data = CommentNotice::query()->with('form_user:id,username,nickname')->where('is_del', 0)->select('id', 'user_id', 'from_user_id', 'msg', 'circle_id', 'is_del', 'created_at', 'read_at')->where('user_id', $user_id)->get();

        return $this->success($data);
    }

    /**
     * @param $pid
     * @return mixed
     * 查询被回复人是who
     */
    protected function selectPid($pid)
    {
        $user_id = Comment::query()->where('id', $pid)->value('user_id');
        return $user_id;
    }
}
